﻿@using RooHui.Infrastructure.Entities.Enum
@using RooHui.Tool
@{
    ViewBag.Title = "InvestmentResult";
    Layout = "~/Views/Shared/_Layout.cshtml";
    RooHui.Portal.Biz.Model.ProjectDetailModel project = ViewBag.ProjectDetail;
    RooHui.Infrastructure.Entities.Entity.Account account = ViewBag.Account;
}
@section head{
    <link rel="stylesheet" type="text/css" href="/content/plugins/fancybox/jquery.fancybox-1.3.4.css" />
    <style type="text/css">
        table.project tr td.title
        {
            width: 25%;
            text-align: right;
            font-size: 16px;
            color: #666;
        }
        table.project tr td.filed
        {
            width: 25%;
            text-align: right;
            font-size: 18px;
        }
        .wall
        {
            padding: 10px;
            border: 1px solid #e3e3e3;
            text-shadow: 0 1px 0 #fff;
            border-radius: 4px;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            -ms-box-sizing: border-box;
        }
        hr
        {
            margin: 10px 0;
        }
    </style>
}
<div class="container" style="padding-top: 20px;">
    <div class="bg-light wall">
        <div class="row-fluid">
            <div class="span8">
                <a href="javascript:;" class="blue" style="font-size: 24px;">@project.ProductName @project.ProjectTitle</a>
                <span style="color: #777; font-size: 12px;">项目编码：@project.ProjectCode</span><br />
                项目融资金额：<span style="color: #e86e00;">￥ @(Math.Round(project.Financing / 10000, 2))
                    万元</span> ， 还需融资额：<span style="color: #3498DB;">￥
                        @(Math.Round(project.RemainFinancing / 10000, 2))
                        万元</span>
                <hr />
                <table class="project" style="width: 90%;">
                    <tr>
                        <td class="title">
                            预期年化利率：
                        </td>
                        <td class="filed">
                            <span class="poptag" style="border-bottom: 1px dashed #333; color: #e86e00;">
                                @(project.Policies.FirstOrDefault(c => !c.IsAttach).Rate)%
                                @if (project.Policies.Any(c => c.IsAttach && c.AddType == PolicyAddType.Award))
                                {
                                    @: + 奖励
                                                                }
                            </span>
                        </td>
                        <td class="title">
                            担保公司：
                        </td>
                        <td class="filed">
                            <img style="width:20px;height:20px;" alt="@project.GuaranteeCompanyName" src="@project.GuaranteeCompanyLogo">
                            @project.GuaranteeCompanyName
                        </td>
                    </tr>
                    <tr>
                        <td class="title">
                            获取收益方式：
                        </td>
                        <td class="filed">
                            <span style="border-bottom: 1px dashed #333;">@project.Policies.FirstOrDefault(c => !c.IsAttach).BalanceType.Mapping()</span>
                        </td>
                        <td class="title">
                            融资期限：
                        </td>
                        <td class="filed">
                            @(project.ProjectCycleAmount)
                            期&nbsp;&nbsp;&nbsp;(@project.Policies.FirstOrDefault(c => !c.IsAttach).Duration
                            @(project.Policies.FirstOrDefault(c => !c.IsAttach).DurationType.Mapping())/期)
                        </td>
                    </tr>
                    <tr>
                        <td class="title">
                            手续费：
                        </td>
                        <td class="filed">
                            0.00 元
                        </td>
                        <td class="title">
                            @(((int)project.ProjectStatus < (int)ProjectStatus.Repayment) ? "(预计)" : "")起息日：
                        </td>
                        <td class="filed">@project.BeginDate.ToString("yyyy-MM-dd")
                        </td>
                    </tr>
                    <tr>
                        <td class="title">
                            融资进度：
                        </td>
                        <td colspan="3">
                            @{
                                var percent = (int)(((project.Financing - project.RemainFinancing) / project.Financing) * 100);
                                var progressStyle = percent <= 50 ? "progress-info" : percent < 100 ? "progress-warning" : "progress-success";
                            }
                            <div class="progress @progressStyle progress-striped" style="margin:0;">
                                <div class="bar" style="width: @percent%">
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
                <hr />
                <div class="row-fluid">
                    @if (project.Policies.Any(c => c.IsAttach && c.AddType == PolicyAddType.Award))
                    {
                        <div class="span5">
                            <ul>
                                <li><strong style="color: Red; font-size: 20px;">奖励：</strong></li>
                                @{
                        var policies = project.Policies.Where(c => c.IsAttach).ToList();
                        foreach (var policy in policies)
                        {
                            if (policy.AddType == PolicyAddType.Award)
                            {
                                    <li><strong style="color: Red;">额外追加 @policy.Rate @(policy.SettlementType == SettlementType.Proportion ? "% 年化收益率" : "固定收益")
                                        (@policy.CycleAmount 期 @policy.Duration @policy.DurationType.Mapping()/期)</strong></li>
                            }
                        }
                                }
                            </ul>
                        </div>
                    }
                    @if (project.Policies.Any(c => c.IsAttach && c.AddType == PolicyAddType.Promotion))
                    {
                        <div class="span7">
                            <ul>
                                <li><strong style="color: Green; font-size: 20px;">享受促销：</strong></li>
                                @{
                        var policies = project.Policies.Where(c => c.IsAttach).ToList();
                        foreach (var policy in policies)
                        {
                            if (policy.AddType == PolicyAddType.Promotion)
                            {
                                    <li><strong style="color: Green;">额外追加 @policy.Rate @(policy.SettlementType == SettlementType.Proportion ? "% 年化收益率" : "固定收益")
                                        (@policy.CycleAmount 期 @policy.Duration @policy.DurationType.Mapping()/期) </strong>
                                    </li>
                            }
                        }
                                }
                            </ul>
                        </div>}
                </div>
            </div>
            <div class="span4">
                <div class="headline headline-md">
                    <h2>
                        合同协议</h2>
                    <ul class="list-unstyled blog-photos" style="margin: 10px 0;">
                        @{
                            var agreement = project.Resource.Where(c => c.ResouceSubType == ResourceSubType.ProjectAgreement).Take(6).ToList();
                            foreach (var item in agreement)
                            {
                                if (item.PrivateLevel == PrivateLevel.Public ||
                                    (item.PrivateLevel == PrivateLevel.MemberOnly && HttpContext.Current.User.Identity.IsAuthenticated))
                                {
                            <li><a class="contract" href="@item.Path" title="@item.Name">
                                <img class="hover-effect" alt="@item.Name" src="@item.Path"></a></li>
                                }
                            }
                        }
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="row-fluid">
        <div class="span12">
            <div class="wall" style="margin-top: 10px; min-height: 220px;">
                <h3>
                    投资详情</h3>
                <hr />
                <div style="font-size: 16px;" class="alert alert-success">
                    <b>您的本次投资成功！</b>
                </div>
                @{
                    List<RooHui.Infrastructure.Entities.Entity.Investment> investment = ViewBag.Investment;
                }
                <table class="table table-bordered">
                    <tr>
                        <td rowspan="@(investment.Count + 1)" width="30%">
                            您在本项目共投资：
                        </td>
                        <td colspan="2">
                            <span id="increaseMoney">合计：￥ @(investment.Sum(c => c.InvestMentAmount))</span>
                            元
                        </td>
                    </tr>
                    @foreach (var i in investment)
                    {
                        <tr>
                            <td colspan="2">
                                于 @i.CreateTime.ToString("yyyy.MM.dd HH:mm") 投资 @i.InvestMentAmount.ToString("F2")
                                元
                            </td>
                        </tr>
                    }
                    <tr>
                        <td>
                            收益方式：
                        </td>
                        <td colspan="2">
                            <span style="border-bottom: 1px dashed #333;">@project.Policies.FirstOrDefault(c => !c.IsAttach).BalanceType.Mapping()</span>
                        </td>
                    </tr>
                    <tr>
                        <td>@(((int)project.ProjectStatus < (int)ProjectStatus.Repayment) ? "(预计)" : "")起息日：
                        </td>
                        <td colspan="2">
                            @project.BeginDate.ToString("yyyy-MM-dd")
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <strong>您的收益计划 （￥ @(investment.Sum(c => c.InvestMentAmount))）：</strong>
                        </td>
                    </tr>
                    @{
                        List<IGrouping<DateTime, RooHui.Portal.Biz.Model.PayBackItemModel>> data = ViewBag.Plan;
                        foreach (var item in data)
                        {
                            for (int i = 0, len = item.Count(); i < len; i++)
                            {
                                var paybackItem = item.ToList();
                        <tr>
                            @if (i == 0)
                            {
                                <td rowspan="@paybackItem.Count()">@paybackItem[i].PayBackDate.ToString("yyyy-MM-dd")
                                </td>
                            }
                            <td>
                                @if (paybackItem[i].AddType == PolicyAddType.Promotion)
                                {
                                    <strong style="color: green">[促销]</strong>
                                }
                                else if (paybackItem[i].AddType == PolicyAddType.Award)
                                {
                                    <strong style="color: red">[奖励]</strong>
                                }
                                @paybackItem[i].Name
                            </td>
                            <td>
                                ￥ @paybackItem[i].Value（元）
                            </td>
                        </tr> 
                            }
                        }
                    }
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td colspan="2" style="color: #e86e00; font-size: 20px; text-align: right;">
                            <strong>合计收益： ￥ @data.Sum(c => c.Sum(i => i.Value)) 元</strong>
                        </td>
                    </tr>
                </table>
            </div>
            
        </div>
    </div>
</div>
@section scripts{
    <script type="text/javascript" src="/content/plugins/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
    <script type="text/javascript">
        $(function () {
            $('.contract').fancybox();
            $('#labproject').addClass('active');
        });
        function doInvest() {
            var b = $('#balancebox').text();
            var iv = $('#txtinvest').val();
            var increase = $('#hidincreaseMoney').val();
            if (!(/^\d+\.\d{1,2}$/.test(b) && /^\d+\.?\d*$/.test(iv))) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 请输入投资金额</span>');
                return;
            }
            else if (parseFloat(b) < parseFloat(iv)) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 您的余额不足，请先充值</span>');
                return;
            }
            else if (parseFloat(iv) <= 0 || parseFloat(iv) % increase != 0) {
                if ($('#txtinvest').siblings('span').length != 0) {
                    $('#txtinvest').siblings('span').remove();
                }
                $('#txtinvest').css({ 'border-color': '#b94a48' }).after('<span style="color:#b94a48;"> 投资金额必须为单份金额的整数倍</span>');
                return;
            }
            else {
                $('#txtinvest').css({ 'border-color': '#ccc' }).siblings('span').remove();
                $('#frminvestment').submit();
            }

        }
        function refrashAccount(sender) {
            $.ajax({
                url: '/account/getAccountInfo',
                data: {
                    u: '@HttpContext.Current.User.Identity.Name'
                },
                cache: false,
                beforeSend: function () {
                    $(sender).attr('disabled', 'disabled').parent().children(':last').after('<i class="loading"></i>');
                },
                success: function (result) {
                    $(sender).siblings('i').remove();
                    $(sender).siblings('b').text(result);
                    $(sender).removeAttr('disabled');
                }
            });
        }
        function getInvestPlan(sender) {
            $.ajax({
                url: '/project/getInvestPlan',
                data: {
                    projectId: '@project.ProjectId',
                    invest: $('#txtinvest').val()
                },
                beforeSend: function () {
                    $('#planbox').html('');
                    $('#increaseMoney').text($('#txtinvest').val());
                    $(sender).removeClass('btn-u-red').addClass('btn-u-default').attr('disabled', 'disabled').after('<i class="loading"></i>');
                },
                success: function (result) {
                    $(sender).removeClass('btn-u-default').addClass('btn-u-red')
                        .removeAttr('disabled');
                    $(sender).siblings('i').remove();
                    var list = eval('(' + result + ')');
                    if (list && list.length > 0) {
                        for (var i = 0, len = list.length; i < len; i++) {
                            for (var j = 0, jlen = list[i].length; j < jlen; j++) {
                                var s = '', datetime = '';
                                datetime = list[i][j].PayBackDate.substring(0, list[i][j].PayBackDate.indexOf('T'));
                                if (list[i].length > 1) {
                                    if (j == 0) {
                                        $('#planbox').append($('<tr><td rowspan = "' + list[i].length + '">' + datetime + '</td><td>' + list[i][j].Name + '</td><td>￥ ' + list[i][j].Value + '（元）</td></tr>'));
                                    } else {
                                        $('#planbox').append($('<tr><td>' + list[i][j].Name + '</td><td>￥ ' + list[i][j].Value + '（元）</td></tr>'));
                                    }
                                }
                                else {
                                    $('#planbox').append($('<tr><td>' + datetime + '</td><td>' + list[i][j].Name + '</td><td>￥ ' + list[i][j].Value + '（元）</td></tr>'));
                                }
                            }
                        }
                        $('#planbox').append($('<tr><td>&nbsp;</td><td colspan="2" style="color: #e86e00; font-size: 20px; text-align: right;"><strong>合计收益： ￥ ' + getsum(list) + ' 元</strong></td></tr>'));
                    }
                }
            });
        }
        function getsum(list) {
            var val = 0;
            for (var i = 0, ilen = list.length; i < ilen; i++) {
                for (var j = 0, jlen = list[i].length; j < jlen; j++) {
                    val += parseFloat(list[i][j].Value, 10);
                }
            }
            return Math.round(val * 100) / 100;
        }
    </script>
}
